﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WorkStation.Frameworks.EasyBus.MessageQueue;
using WorkStation.Frameworks.EasyBus.Serializable;

using Microsoft.Practices.ServiceLocation;

namespace WorkStation.Frameworks.EasyBus.Extensions
{
    public static class ServiceContainerExtensions
    {
        /// <summary>
        /// 使用 ServiceLocator 当组件容器
        /// </summary>
        /// <param name="container">服务容器</param>
        /// <param name="translator">默认的地址翻译对象</param>
        /// <returns></returns>
        public static ServiceContainer UseServiceLocator(this ServiceContainer container, params IQueueAddressTranslator[] translator) 
        {
            container.RegisterCollection<IQueueAddressTranslator>(() => ServiceLocator.Current.GetAllInstances<IQueueAddressTranslator>(), () => translator);

            // 使用 ServiceLocator 当服务容器，不设置的话，默认使用 DefaultMessageSerializable 序列化消息
            container.Register<IMessageSerializable>(() => ServiceLocator.Current.GetInstance<IMessageSerializable>(), () => new DefaultMessageSerializable());

            return container;
        }
    }
}
